package com.bianguo.android.bprinter.utils;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGattServerCallback;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.bianguo.android.baselibrary.utils.Contacts;
import com.bianguo.android.baselibrary.utils.PermissionHelper;

import java.util.ArrayList;
import java.util.Set;

public class BluetoothHelper {

    private static volatile BluetoothHelper mHelper;
    private BluetoothManager bluetoothManager;
    private Context mContext;

    private BluetoothHelper(Context mContext) {
        this.mContext = mContext.getApplicationContext();
        bluetoothManager = (BluetoothManager) this.mContext.getSystemService(Context.BLUETOOTH_SERVICE);
    }

    public static BluetoothHelper getInstance(Context mContext) {
        BluetoothHelper bluetoothHelper = mHelper;
        if (bluetoothHelper == null) {
            synchronized (PermissionHelper.class) {
                if (bluetoothHelper == null) {
                    bluetoothHelper = new BluetoothHelper(mContext.getApplicationContext());
                    mHelper = bluetoothHelper;
                }
            }
        }
        return bluetoothHelper;
    }

    public void addGattCallback(BluetoothGattServerCallback callback) {
        bluetoothManager.openGattServer(mContext, callback);
    }

    /**
     * 打开蓝牙
     * @param activity
     */
    public void open(Activity activity) {
        //本地蓝牙的适配器，也就是说当前应用程序所运行的Android设备上的蓝牙
        BluetoothAdapter adapter = bluetoothManager.getAdapter();
        if (adapter == null) {
            Toast.makeText(activity, "设备不支持蓝牙功能", Toast.LENGTH_SHORT).show();
            return;
        }
        if (adapter.isEnabled()) {
            //表示蓝牙已打开
            return;
        }
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        activity.startActivityForResult(enableBtIntent, Contacts.START_ACTIVITY_RESULT_BLUETOOTH);
        //静默开启蓝牙
//        adapter.enable();
    }

    /**
     * 获取所有所搜到的远程蓝牙
     * 远程的蓝牙适配器，也就是说你要连接的Android设备的适配器。
     * @return
     */
    public ArrayList<BluetoothDevice> getDevices () {
        Set<BluetoothDevice> devices = getAdapter().getBondedDevices();
        if (devices != null && devices.size() > 0) {
            return new ArrayList<>(devices);
        }
        return null;
    }

    /**
     * 搜索蓝牙
     */
    public void searchDevices() {
        BluetoothAdapter adapter = bluetoothManager.getAdapter();
        if (adapter == null) {
            return;
        }
        if (!adapter.isEnabled()) {
            return;
        }
        if (adapter.isDiscovering()) {
            adapter.cancelDiscovery();
        }
        adapter.startDiscovery();
    }

    public void stopSearch() {
        BluetoothAdapter adapter = bluetoothManager.getAdapter();
        if (adapter == null) {
            return;
        }
        if (!adapter.isEnabled()) {
            return;
        }
        if (adapter.isDiscovering()) {
            adapter.cancelDiscovery();
        }
    }

    public BluetoothAdapter getAdapter() {
        return bluetoothManager.getAdapter();
    }
}
